// 8085Doc.cpp : implementation of the CMy8085Doc class
//

#include "stdafx.h"
#include "8085.h"
#include "8085Doc.h"
#include "CommonObject.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMy8085Doc

IMPLEMENT_DYNCREATE(CMy8085Doc, CDocument)

BEGIN_MESSAGE_MAP(CMy8085Doc, CDocument)
	//{{AFX_MSG_MAP(CMy8085Doc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


BOOL	CMy8085Doc::m_bMiniHelp				= TRUE;
BOOL	CMy8085Doc::m_bSyntaxHighlighting	= TRUE;

/////////////////////////////////////////////////////////////////////////////
// CMy8085Doc construction/destruction

CMy8085Doc::CMy8085Doc() : m_xTextBuffer(this)
{
	// TODO: add one-time construction code here
    m_Parser.Set8085Object(Get8085Object());
    Init();
	//	Initialize LOGFONT structure
	memset(&m_lf, 0, sizeof(m_lf));
	m_lf.lfWeight = FW_NORMAL;
	m_lf.lfCharSet = ANSI_CHARSET;
	m_lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	m_lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	m_lf.lfQuality = DEFAULT_QUALITY;
	m_lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
	strcpy(m_lf.lfFaceName, "Courier");
}

CMy8085Doc::~CMy8085Doc()
{
    m_cDebugInfo.Close();
}

BOOL CMy8085Doc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	m_xTextBuffer.InitNew();

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CMy8085Doc serialization

void CMy8085Doc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
	
	// CEditView contains an edit control which handles all serialization
	// ((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CMy8085Doc diagnostics

#ifdef _DEBUG
void CMy8085Doc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMy8085Doc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMy8085Doc commands


void CMy8085Doc::ParseFile(const CString& fileName)
{
    m_Parser.ParseFile(fileName);
}

CParse *  CMy8085Doc::GetParserObject(void)    
{ 
    return &m_Parser; 
}

C8085Object *  CMy8085Doc::Get8085Object(void) 
{ 
    return &m_8085Object; 
}

void CMy8085Doc::SetDisplayOpen(BOOL set) 
{ 
    m_b8085DisplayOpen=set;
}

BOOL CMy8085Doc::IsDisplayOpen() 
{ 
    return m_b8085DisplayOpen;
}

// return TRUE if keyboard inputs are to be ignored
// The m_bIgnoreInputs is set to TRUE while Animating
BOOL CMy8085Doc::IgnoreInputs() 
{ 
    return m_bIgnoreInputs;
}

// change the m_bIngnoreInputs Flag.
// if set to TRUE most keyboard input are ignored
void CMy8085Doc::SetIgnoreInputs(BOOL bIgnore)
{
    m_bIgnoreInputs=bIgnore;
}

// sets the animation flag
void CMy8085Doc::SetAnimating(BOOL bAnimating)
{
    m_bIsAnimating = bAnimating;
}

// return TRUE if the machine is running
BOOL CMy8085Doc::IsAnimating()
{
    return m_bIsAnimating;
}

void CMy8085Doc::Init()
{
    SetDisplayOpen(FALSE);
    m_bIsAnimating = FALSE;
    m_bIgnoreInputs = FALSE;
}

void CMy8085Doc::OnCloseDocument() 
{
	// TODO: Add your specialized code here and/or call the base class

    if(IsDisplayOpen())
        return; // don't close if the simulator display is open
	CDocument::OnCloseDocument();
}

// all breakpoint positions are replaced
// by the breakpoint instruction
void CMy8085Doc::InsertBkPtInCode(BOOL bRefresh)
{
    OneBreakPoint* pt;

    if(bRefresh)
    {
        // refresh the data opcodes stored in the breakpoint
        // array before inserting the breakpoints
        for(pt=FirstBreakPoint(); pt ; pt = NextBreakPoint() )
        {
            pt->data = Get8085Object()->ReadMemByte(pt->address);
        }
    }    
    for(pt=FirstBreakPoint(); pt ; pt = NextBreakPoint() )
    {
        if(pt->bEnabled)
            Get8085Object()->WriteMem(pt->address,
                Get8085Object()->GetBreakPtOpCode());
    }
}

// remove the breakpoint instruction from the code and restore
// the opcode which was replaced by the breakpoint instruction
void CMy8085Doc::RemoveBkPtFromCode()
{
    OneBreakPoint* pt;

    for(pt=FirstBreakPoint(); pt ; pt = NextBreakPoint() )
    {
        Get8085Object()->WriteMem(pt->address,pt->data);
    }
}

void CMy8085Doc::AddBkPt(word addr,int nSrcLine, BOOL initial_state )
{
    ASSERT(Get8085Object());
    byte op=Get8085Object()->ReadMemByte(addr);
    AddBreakPoint(addr,op,nSrcLine, initial_state );
}


void CMy8085Doc::ToggleBkPt(word addr, int nSrcLine)
{
    if(DeleteBkPt(addr))
        return;
    AddBkPt(addr,nSrcLine);
}

void CMy8085Doc::DeleteContents() 
{
	// TODO: Add your specialized code here and/or call the base class
	CDocument::DeleteContents();
	m_xTextBuffer.FreeAll();
}

BOOL CMy8085Doc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	// TODO: Add your specialized creation code here
	
	return m_xTextBuffer.LoadFromFile(lpszPathName);
	// return TRUE;
}

BOOL CMy8085Doc::OnSaveDocument(LPCTSTR lpszPathName) 
{
	// TODO: Add your specialized code here and/or call the base class
	m_xTextBuffer.SaveToFile(lpszPathName);
	return TRUE;	//	Note - we didn't call inherited member!
	
	// return CDocument::OnSaveDocument(lpszPathName);
}

void CMy8085Doc::OnReadOnly() 
{
	if (! m_xTextBuffer.GetReadOnly())
	{
		m_xTextBuffer.SetReadOnly(TRUE);
		AfxMessageBox("Document now read-only!");
	}
	else
		m_xTextBuffer.SetReadOnly(FALSE);
}

void CMy8085Doc::OnUpdateReadOnly(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_xTextBuffer.GetReadOnly());
}